﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class Comod_StockManage : ATPages
{

    ZSql zs = new ZSql(); 
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
           
            Hidid.Value = Request.Cookies["user"].Values["id"];
            HFroid.Value = Request.Cookies["user"].Values["roleid"];
            Hidware.Value = getWareId();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            stokid.Value = Request["id"];
            initData();
            CheckComod();
            f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 0, GvData);
            //Label2.Text=Request.Cookies["user"].Values["roleid"];
        }
    }

    private void initData()
    {
        string sql = "";

        sql = "select a.stockId,c.ComoditiesType,WarehouseName,d.Brand,e.ComoditiesName,f.Specifications,m.GuestId,m.MaintDate,m.MaintCode,m.SalesCode,m.DevicState,m.AuditUserName,m.Maintid from CS_stock a join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId join CS_zk m on m.deptid=a.stockId join CS_Warehouse b on m.housenameid=b.WarehouseId where a.WarehouseId<>'24' and  a.stockId =" + Request["id"] + " order by c.ComoditiesType";

        //if (Hidware.Value=="0")
        //{
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
        //}
        //else
        //{
        //    PageNavigator1.Total=1;
        //    PageNavigator1.Query = sql += " where a.warehouseid=" + Hidware.Value + "  order by warehousename";
        //}
        //zs.Open();

        //GvData.DataSource = zs.m_table;
        //GvData.DataBind();
    }
    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Hidid.Value;
        zs.Open(sql);
        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sql = "";
        sql = "select a.stockId,c.ComoditiesType,WarehouseName,d.Brand,e.ComoditiesName,f.Specifications,m.GuestId,m.MaintDate,m.MaintCode,m.SalesCode,m.DevicState,m.AuditUserName,m.Maintid from CS_stock a join ComoditiesType c on a.ComoditiesType=c.TypeId join Brand d on a.BrandId=d.BrandId join ComoditiesName e on a.ComoditiesName=e.ComoditiesNameId join Specifications f on a.ModelId=f.SpecificationsId join Units j on a.UnitId=j.UnitsId join CS_Supplier h on a.SuppliId=h.SupplierId join CS_zk m on m.deptid=a.stockId join CS_Warehouse b on m.housenameid=b.WarehouseId where a.WarehouseId<>'24' and  a.stockId =" + stokid.Value + " ";
        if (!string.IsNullOrEmpty(txttype.Text))
        {
            sql += " and c.ComoditiesType like'%" + txttype.Text + "%'";
        }
        if (!string.IsNullOrEmpty(txtband.Text))
        {
            sql += " and d.Brand like'%" + txtband.Text + "%'";
        }
        if (!string.IsNullOrEmpty(txtModul.Text))
        {
            sql += " and f.Specifications like'%" + txtModul.Text + "%'";
        }
        if (ddrzt.SelectedValue!="0")
        {
            sql += " and m.AuditUserName ='" + ddrzt.SelectedValue + "'";
        }
        if (!string.IsNullOrEmpty(txtname.Text))
        {
            sql += " and e.ComoditiesName like'%" + txtname.Text + "%'";
        }
        sql += " order by c.ComoditiesType";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    private void CheckComod()
    {
        for (int i = 0; i < GvData.Rows.Count; i++)
        {
            if (Convert.ToInt32(GvData.Rows[i].Cells[10].Text) < 20)
            {
                GvData.Rows[i].Cells[10].ForeColor = Color.Red;
            }
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[9].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = (this.GvData.Rows[i].Cells[0].FindControl("Label1") as Label).Text;
                    zs.Open("delete CS_Outbj  where Maintid='" + id + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string all = "tj";
        if (ddrzt.SelectedValue=="0" && txtModul.Text == "" && txtband.Text == "" && txttype.Text == "" && txtname.Text == "")
        {
            all = "all";
        }
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData('" + all + "','" + txttype.Text + "','" + txtband.Text + "','" + txtname.Text + "','" + txtModul.Text + "','" + ddrzt.SelectedValue + "','"+stokid.Value+"')", true);
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"],btnDel);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string stockId = DataBinder.Eval(e.Row.DataItem, "AuditUserName").ToString();
            if (Convert.ToInt32(stockId) == 1)
            {
                e.Row.Cells[9].Text = "进货";

            }
            if (Convert.ToInt32(stockId) == 2)
            {
                e.Row.Cells[9].Text = "转库";
            }
            if (Convert.ToInt32(stockId) == 3)
            {

                e.Row.Cells[9].Text = "退货";
            }
        }
    }
}
